package com.uqbar.game;

import java.util.List;
import com.uqbar.game.asteroid.tipe.Asteroid;
import com.uqbar.game.random.RandomAsteroid;
import com.uqbar.game.scene.GameAsteroid;
import com.uqbar.game.stateSpacecraft.StateDestroy;
import com.uqbar.game.stateSpacecraft.StateInvisibility;
import com.uqbar.game.stateSpacecraft.StateSpacecraft;
import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.appearances.Animation;
import com.uqbar.vainilla.appearances.Sprite;

public class Spacecraft extends AsteroidComponent {
	private double speed = 0;
	private double maxSpeed = 0.7;
	private int diameter = 20;
	private int aceleracion = 1;
	private double movX, movY;
	private double angle;
	private RandomAsteroid r;
	private int life = 3;
	private int score = 0;
	private StateSpacecraft state;
	private Sprite[] spacecraftExploitSprites = new Sprite[8];
	private Animation spacecraftExploitAnimation;
	private GameAsteroid gameAsteroid;
	
	public Spacecraft(RandomAsteroid r, GameAsteroid gameAsteroid) {
		this.movX = 0;
		this.movY = -1;
		this.angle = 44;
		this.r = r;
		this.gameAsteroid = gameAsteroid;
		this.state = new StateInvisibility(this);
	}

	@Override
	public void update(DeltaState deltaState) {
		super.update(deltaState);
		
		this.checkStatus();
		
		this.state.moveSpacecraft(deltaState);
	}
	
	private void checkStatus() {
		if(r.sizeEqualsZero()){
			this.gameAsteroid.win();
			this.destroyBullets();
			this.state = new StateDestroy(this);
		}
		if(this.getLife() == 0){
			this.gameAsteroid.lost();
		}
	}

	private void destroyBullets() {
		for (Bullet bullet : this.state.getBullets()) {
			bullet.destroy();
		}
	}

	public void changeToInvisible(){
		this.setAppearance(this.state.getImage());
	}
	
	public void changeToPlayable(){
		this.setAppearance(this.state.getImage());
	}

	public void rotate(DeltaState deltaState) {
		
		Sprite currentSpacecraft = this.state.getImage();
		
		currentSpacecraft = currentSpacecraft.rotate(angle);
		this.setAppearance(currentSpacecraft);
	}
	
	public void initializeAnimationExploit(){
		//inicializar los sprite
		for(int i= 0; i<8; i++){
			spacecraftExploitSprites[i]=Sprite.fromImage("/explosions/explosion_"+i+".png");
		}
		
		this.spacecraftExploitAnimation = new Animation(0.2, this.spacecraftExploitSprites);
	}

	public void center(){
		setX(400);
		setY(300);
	}
	public void exploit() {
		this.destroy();
	}

	public void lostOneLife() {
		this.life--;
	}
	
	public void addScore(int score) {
		this.score += score;
	}
	
	/*-***********GETERS AND SETERS***********-*/
	public void setMovX(double movX) {
		this.movX = movX;
	}

	public double getMovX() {
		return movX;
	}

	public void setMovY(double movY) {
		this.movY = movY;
	}

	public double getMovY() {
		return movY;
	}
	public int getDiameter() {
		return diameter;
	}

	public void setDiameter(int diameter) {
		this.diameter = diameter;
	}
	
	public double getSpeed() {
		return speed;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
	}

	public double getMaxSpeed() {
		return maxSpeed;
	}

	public void setMaxSpeed(double maxSpeed) {
		this.maxSpeed = maxSpeed;
	}

	public int getAceleracion() {
		return aceleracion;
	}

	public void setAceleracion(int aceleracion) {
		this.aceleracion = aceleracion;
	}

	public double getAngle() {
		return angle;
	}

	public void setAngle(double angle) {
		this.angle = angle;
	}

	public int getLife() {
		return life;
	}

	public void setLife(int life) {
		this.life = life;
	}

	public StateSpacecraft getState() {
		return state;
	}

	public void setState(StateSpacecraft state) {
		this.state = state;
	}

	public void setR(RandomAsteroid r) {
		this.r = r;
	}

	public RandomAsteroid getR() {
		return r;
	}

	public List<Asteroid> getAsteroids() {
		return r.getAsteroids();
	}
	public Sprite[] getSpacecraftSprites() {
		return spacecraftExploitSprites;
	}

	public void setSpacecraftSprites(Sprite[] spacecraftSprites) {
		this.spacecraftExploitSprites = spacecraftSprites;
	}

	public Animation getSpacecraftAnimation() {
		return spacecraftExploitAnimation;
	}

	public void setSpacecraftAnimation(Animation spacecraftAnimation) {
		this.spacecraftExploitAnimation = spacecraftAnimation;
	}
	public Sprite[] getSpacecraftExploitSprites() {
		return spacecraftExploitSprites;
	}

	public void setSpacecraftExploitSprites(Sprite[] spacecraftExploitSprites) {
		this.spacecraftExploitSprites = spacecraftExploitSprites;
	}

	public Animation getSpacecraftExploitAnimation() {
		return spacecraftExploitAnimation;
	}

	public void setSpacecraftExploitAnimation(Animation spacecraftExploitAnimation) {
		this.spacecraftExploitAnimation = spacecraftExploitAnimation;
		
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getScore() {
		return score;
	}
}
